package com.csnf.dao;

import com.csnf.entity.User;
import com.csnf.util.JdbcUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    /**
     * 全部查询
     * @return
     * @throws SQLException
     */
    public List<User> getAll() throws SQLException {
//        Connection conn = DriverManager.getConnection("jdbc:mysql:///hymalldb", "root", "root123");
//        PreparedStatement ps = conn.prepareStatement("select * from user");
        Connection conn = JdbcUtil.getConnection();
        String sql = "select user_id,user_name,user_nickname from user";
        PreparedStatement ps = JdbcUtil.getPreparedStatement(conn,sql);
        ResultSet rs = ps.executeQuery();
        List<User> userList = new ArrayList<>();
        while(rs.next()){
//            User user =new User() ;
//            user.setUser_id(rs.getInt("user_id"));
//            user.setUser_name(rs.getString("user_name"));
//            user.setUser_nickname(rs.getString("user_nickname"));
            User user = null;
            try {
                user = JdbcUtil.invokeObject(rs,User.class);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            userList.add(user);
        }
        JdbcUtil.close(rs,ps,conn);
        return userList;
    }

    /**
     * 通过用户名查询
     * @param nickname
     * @return
     * @throws SQLException
     */
    public List<User>getUserByNickname(String nickname) throws SQLException {
//        Connection conn = DriverManager.getConnection("jdbc:mysql:///hymalldb", "root", "root123");
//        PreparedStatement ps = conn.prepareStatement("select * from user where user_nickname like ?");
        Connection conn = JdbcUtil.getConnection();
        String sql = "select user_id,user_name,user_nickname from user where user_nickname like ?";
        PreparedStatement ps = JdbcUtil.getPreparedStatement(conn,sql);
        ps.setString(1, "%"+nickname+"%");
        ResultSet rs = ps.executeQuery();
        List<User> userList = new ArrayList<>();
        while(rs.next()){
//            User user =new User() ;
//            user.setUser_id(rs.getInt("user_id"));
//            user.setUser_name(rs.getString("user_name"));
//            user.setUser_nickname(rs.getString("user_nickname"));
            User user = null;
            try {
                user = JdbcUtil.invokeObject(rs,User.class);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            userList.add(user);
        }
        JdbcUtil.close(rs,ps,conn);
        return userList;
    }
    /**
     * 通过用户ID查询
     */
    public User grtUserById(Integer user_id) throws Exception {
        Connection conn = JdbcUtil.getConnection();
        String sql = "select user_id,user_name,user_nickname from user where user_id = ?";
        PreparedStatement ps = JdbcUtil.getPreparedStatement(conn,sql,user_id);
        ResultSet rs = ps.executeQuery();
        User user = null;
        if (rs.next()){
                user = JdbcUtil.invokeObject(rs, User.class);
            }
        JdbcUtil.close(rs,ps,conn);
        return user;
    }

//    public boolean getUserName(Integer user_id, String user_name, String user_nickname) throws Exception {
//
//            Connection conn = null;
//            PreparedStatement ps = null;
//            boolean isUpdated = false;
//
//            try {
//                // 获取数据库连接
//                conn = JdbcUtil.getConnection();
//
//                // 构建SQL语句
//                String sql = "UPDATE user SET user_name=?, user_nickname=? WHERE user_id=?";
//                ps = JdbcUtil.getPreparedStatement(conn, sql, user_name, user_nickname, user_id);
//
//                // 执行更新操作
//                int rowsAffected = ps.executeUpdate();
//
//                // 检查是否有受影响的行数来判断是否更新成功
//                if (rowsAffected > 0) {
//                    isUpdated = true;
//                }
//
//            } catch (Exception e) {
//                throw e;
//            } finally {
//                // 关闭资源
//                JdbcUtil.close( ps, conn);
//            }
//
//            return isUpdated;
//        }

        public boolean updateUserName(User user) throws Exception {
            String sql = "UPDATE user SET user_name=?, user_nickname=? WHERE user_id=?";
            Connection conn = JdbcUtil.getConnection();
            PreparedStatement ps = JdbcUtil.getPreparedStatement(conn, sql, user.getUser_name(), user.getUser_nickname(), user.getUser_id());
            int pow = ps.executeUpdate();
            JdbcUtil.close(ps, conn);
            return pow > 0;
        }
    }

